package functors;

import domain.Point;
import util.ShuduUtil;

import java.util.List;

/**
 * @Author: sszheng
 * @Date: 2018/8/6 16:48
 */
public class ThreeLineFunctor implements Functor {
    @Override
    public Boolean calc( List<Point> list) {
        Boolean flag = Boolean.FALSE;
        for(Point point: list) {
            if(point.getValue() == 0) {
                flag = flag | calcPointFrom3Line(point, list);
            }
        }
        return flag;
    }

    private Boolean calcPointFrom3Line(Point point, List<Point> list) {
        Boolean flag = Boolean.FALSE;
        for(Point point1: list) {
            if(point1.equals(point)) {
                continue;
            }
            if(point1.getValue() == 0) {
                continue;
            }
            if(point1.getX() == point.getX()) {
                point.getPossibleValue().remove(point1.getValue());
            }
            if(point1.getY() == point.getY()) {
                point.getPossibleValue().remove(point1.getValue());
            }
            if(ShuduUtil.isPart(point, point1)) {
                point.getPossibleValue().remove(point1.getValue());
            }
        }
        Integer value = ShuduUtil.getUniqueValue(point.getPossibleValue());
        if(value != null) {
            point.setValue(value);
            flag = Boolean.TRUE;
        }

        return flag;
    }
}
